﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebeTerm.WebControls
{
    public abstract class ListControlBase : ControlBase
    {
        //public List<Query> Query { get; set; }
        public string List { get; set; }
        public Dictionary<string, object> Conditions
        {
            get { return Parameters == null ? null : GetObject(Parameters, "Conditions") as Dictionary<string, object>; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected WebeTerm.Common.Pages BuildPages()
        {
            if (Parameters == null)
                return null;

            WebeTerm.Common.Pages pages = new WebeTerm.Common.Pages();
            pages.RecordPaginal = Convert.ToInt32(GetValue(Parameters, "PageSize"));
            pages.PageNumber = Convert.ToInt32(GetValue(Parameters, "PageNo"));

            List<object> sorts = (GetObject(Parameters, "Sorts") as object[]).ToList();
            foreach (Dictionary<string, object> everySort in sorts)
            {
                string sortName = GetValue(everySort, "SortName");
                if (Convert.ToInt32(GetValue(everySort, "SortType")) == 1)
                    pages.AddSortField(sortName);
                else
                    pages.AddConverseField(sortName);
            }

            return pages;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                ExcuteCommand();
                LoadData();
            }
        }

        protected virtual void ExcuteCommand()
        {

        }

        protected virtual void LoadData()
        {

        }

        /*
        protected T GetPara<T>(List<Query> query, string name)
        {
            if (query.Exists(x => x.Name == name))
            {
                object obj = query.Find(x => x.Name == name).Value;
                if (obj != null && !String.IsNullOrEmpty(obj.ToString()))
                {
                    if (typeof(T) == typeof(int?))
                        return (T)(object)Utility.ParseInt(obj.ToString());

                    if (typeof(T) == typeof(bool?))
                        return (T)(object)Utility.ParseBool(obj.ToString());

                    if (typeof(T) == typeof(DateTime?))
                        return (T)(object)Utility.ParseDate(obj.ToString());

                    if (typeof(T) == typeof(decimal?))
                        return (T)(object)Utility.ParseDecimal(obj.ToString());

                    if (typeof(T) == typeof(Guid))
                        return (T)(object)new Guid(obj.ToString());

                    return (T)obj;
                }
                else
                {
                    if (typeof(T) == typeof(Guid))
                        return (T)(object)Guid.Empty;
                    else
                        return (T)(object)null;
                }
            }
            else
            {
                if (typeof(T) == typeof(Guid))
                    return (T)(object)Guid.Empty;
                else
                    return (T)(object)null;
            }
        }
        */
    }
}
